home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2 (Special) / PCPro-2b.iso / Demos / Macromedia / CourseBuilder / CourseBuilderInstaller.exe / Disk1 / data1.cab / Dreamweaver-unInstalled / Configuration / Commands / Add Interaction To Gallery.js < prev    next >
Encoding:
JavaScript  |  1999-12-06  |  12.0 KB  |  434 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBALS  *****************
  4.  
  5. var HELP_DOC = HELP_cmdSaveToGallery;
  6.  
  7. var KO_LIST = '';
  8. var FULL_SRC = '';
  9. var DEFAULT_IMG = '';
  10.  
  11. var CAT_PICK = '';
  12. var OBJ_PICK = '';
  13.  
  14. var CAT_TYPE = '';
  15. var CAT_LIST = '';
  16. var CAT_NAME = '';
  17. var BROWSER_TYPE = '';
  18. var OBJ_TYPE = '';
  19. var OBJ_LIST = '';
  20. var OBJ_NAME = '';
  21. var IMG_ICON = '';
  22. var IMG_NAME = '';
  23.  
  24.  
  25. //******************* API **********************
  26.  
  27. function commandButtons(){
  28.   return new Array(BTN_OK,"cmdOK()",
  29.                    BTN_Cancel,"cmdCancel()",
  30.                    BTN_Help, "displayHelp()");
  31. }
  32.  
  33.  
  34. function cmdOK() {
  35.   if (saveKO()) {
  36.     garbageCollect(true);
  37.     window.close();
  38.   }
  39. }
  40.  
  41. function cmdCancel() {
  42.   garbageCollect(true);
  43.   window.close();
  44. }
  45.  
  46.  
  47. function canAcceptCommand() {
  48.   var theSel, retVal = false;
  49.   var docDOM = dw.getDocumentDOM();
  50.   if (docDOM) {
  51.     retVal = (getEnclosingAttainObj() != null);
  52.     if (!retVal) {
  53.       theSel = docDOM.getSelection(); 
  54.       retVal = isValidAttainObjSelection(theSel);
  55.     }
  56.   }
  57.   return retVal;
  58. }
  59.  
  60.  
  61. //***************** LOCAL FUNCTIONS  ******************
  62.  
  63. //Returns the simple name of the template or category file name
  64. function getSimpleName(theName) {
  65.   var index, retVal = theName;
  66.   index = retVal.search(/\d\d\d_/);
  67.   if (index == 0)
  68.     retVal = retVal.substring(4);
  69.   index = retVal.search(/_\d\d\.agt/);
  70.   if (index >= 0)
  71.     retVal = retVal.substring(0, index);
  72.   return retVal;
  73. }
  74.  
  75.  
  76. function getFullCatName(theName) {
  77.   var retVal = theName;
  78.   if (retVal.search(/\d\d\d_/) != 0) {
  79.     var curNum, prefixNum = 100;
  80.     var templateDir = new File(FILE_templateUrl);
  81.     var dirList = templateDir.list("*.*", "dirs");
  82.     if (dirList) {
  83.       for (var i=0; i < dirList.length; i++) {
  84.         if (getSimpleName(dirList[i]) == theName) {
  85.           retVal = dirList[i];
  86.           return retVal;
  87.         }
  88.         curNum = parseInt(dirList[i].substring(0,3));
  89.         if (curNum != 'NaN' && curNum >= prefixNum) prefixNum = curNum + 1;
  90.       }
  91.     }
  92.     retVal = prefixNum.toString() + "_" + theName;
  93.   }
  94.   return retVal;
  95. }
  96.  
  97.  
  98. function getFullObjName(theName, theCategory) {
  99.   var retVal = theName;
  100.   if (retVal.search(/\d\d\d_/) != 0) {
  101.     var curNum, prefixNum = 100;
  102.     var category = new File(FILE_templateUrl + File.separator + theCategory);
  103.     var fileList = category.list("*.agt");
  104.     if (fileList) {
  105.       for (var i=0; i < fileList.length; i++) {
  106.         if (getSimpleName(fileList[i]) == theName) {
  107.           retVal = fileList[i];
  108.           return retVal;
  109.         }
  110.         curNum = parseInt(fileList[i].substring(0,3));
  111.         if (curNum != 'NaN' && curNum >= prefixNum) prefixNum = curNum + 1;
  112.       }
  113.     }
  114.     retVal = prefixNum.toString() + "_" + theName;
  115.   }
  116.   if (retVal.search(/_\d\d\.agt/) == -1) {
  117.     if (BROWSER_TYPE[0].checked)
  118.       retVal += "_04.agt";
  119.     else
  120.       retVal += "_03.agt";
  121.   }
  122.   return retVal;
  123. }
  124.  
  125.  
  126. function saveKO() {
  127.   var retVal = false;
  128.   var category = '';
  129.   var object = '';
  130.   var theDir;
  131.   var templateFile, template;
  132.   var imageFile, image, targetImage, target;
  133.   
  134.   // check that we have complete information
  135.   if (CAT_TYPE[0].checked) {
  136.     if (!CAT_LIST.id) {
  137.       alert(MSG_enterCat);
  138.       return retVal;
  139.     }
  140.     category = CAT_LIST.id;
  141.   } 
  142.   if (CAT_TYPE[1].checked) {
  143.     if (CAT_NAME.value.search(/\w/) == -1) {
  144.       alert(MSG_enterCat);
  145.       return retVal;
  146.     }
  147.     category = CAT_NAME.value;
  148.   }
  149.   if (OBJ_TYPE[0].checked) {
  150.     if (!OBJ_LIST.id) {
  151.       alert(MSG_enterObj);
  152.       return retVal;
  153.     }
  154.     templateFile = OBJ_LIST.id.template;
  155.   }
  156.   if (OBJ_TYPE[1].checked) {
  157.     var val0 = legalizeObjName(OBJ_NAME.value);
  158.     if (val0.search(/\w/) == -1) {
  159.       alert(MSG_enterObj);
  160.       return retVal;
  161.     } else if (beginsWithNumber(val0)) {
  162.       alert(MSG_nameMustStartWithLetter);
  163.       return retVal;
  164.     }
  165.     OBJ_NAME.value = val0;
  166.     object = val0;
  167.   }
  168.   if (IMG_NAME.search(/\w/) != -1) {
  169.     image = new File(IMG_NAME);
  170.   } else {
  171.     image = new File(DEFAULT_IMG);
  172.   }
  173.   if (!image.exists()) {
  174.     alert(errMsg(MSG_selectImage, image.getAbsolutePath()));
  175.     return retVal;
  176.   }
  177.   
  178.   // create the dir if needed
  179.   if (CAT_TYPE[1].checked) {
  180.     category = getFullCatName(category);
  181.     var theDir = new File(FILE_templateUrl + File.separator + category);
  182.     if (theDir.exists()) {
  183.       alert(errMsg(MSG_categoryExists, getSimpleName(category)));
  184.       return retVal;
  185.     } else {
  186.       theDir.mkdir();
  187.     }
  188.   }
  189.   
  190.   // get the full object name
  191.   if (OBJ_TYPE[1].checked) {
  192.     object = getFullObjName(object, category);
  193.     // check that it doesn't exist
  194.     templateFile = FILE_templateUrl + File.separator + category + File.separator + object;
  195.     template = new File(templateFile);
  196.     if (template.exists()) {
  197.       alert(errMsg(MSG_objExists, OBJ_NAME.value));
  198.       return retVal;
  199.     }
  200.   } else {    
  201.     // save the source to the template file
  202.     template = new File(templateFile);
  203.     if (template.exists()) {
  204.       if (!template.canWrite()) {
  205.         alert(errMsg(MSG_objExists, template.getName()));
  206.         return retVal;
  207.       } else if (!confirm(errMsg(MSG_replaceExisting, getSimpleName(template.getName())))) {
  208.         return retVal;
  209.   } } } 
  210.   template.setContents(FULL_SRC);
  211.   
  212.   // copy the image file, if specified, or if it does not exist.
  213.   targetImage = templateFile.replace(/\.agt/, ".gif");
  214.   target = new File(targetImage);
  215.   if (!target.exists() || IMG_NAME.search(/\w/) != -1) {    
  216.     if (target.exists()) {
  217.       target.remove();
  218.     }
  219.     image.copyTo(target.getAbsolutePath());
  220.   }
  221.   
  222.   // set the name of the template for the object on the page
  223.   if (OBJ_TYPE[1].checked && KO_LIST.length > 0) {
  224.     for (var i=0; i < KO_LIST.length; i++) {
  225.       KO_LIST[i].setAttribute(TEMPLATE_ATTR, category + File.separator + object);
  226.   } }
  227.   
  228.   retVal = true;
  229.   return retVal;
  230. }
  231.  
  232.  
  233. //Set the list of categories
  234. //
  235. function setCategoryList() {
  236.   var templateDir = new File(FILE_templateUrl);
  237.   var dirList = templateDir.list("*.*", "dirs");
  238.   var dirInfo = '';
  239.   if (dirList) {
  240.     dirInfo = new Array();
  241.     dirInfo.ids = new Array();
  242.     dirInfo.names = new Array();
  243.     for (var i=0; i < dirList.length; i++) {
  244.       if (dirList[i].search(/\d\d\d_/)==0) {
  245.         dirInfo.names.push(getSimpleName(dirList[i]));
  246.         dirInfo.ids.push(dirList[i]);
  247.     } }
  248.   } else {
  249.     dirInfo = new Array();
  250.     dirInfo.ids = new Array('');
  251.     dirInfo.names = new Array(LABEL_noCategories);
  252.   }
  253.   CAT_LIST.setInfo(dirInfo.names, dirInfo.ids);
  254.   if (CAT_LIST.pickValue(CAT_PICK) && CAT_PICK)
  255.     CAT_TYPE[0].checked = true;
  256.   else
  257.     CAT_TYPE[1].checked = true;
  258. }
  259.  
  260.  
  261. //Set the list of objects with the files that are writable.
  262. //
  263. function setObjectList() {
  264.   var fileInfo = new Array();
  265.   if (CAT_TYPE[0].checked) {
  266.     var category = new File(FILE_templateUrl + File.separator + CAT_LIST.id);
  267.     var filter = (BROWSER_TYPE[0].checked) ? "*.agt" : "*_03.agt";
  268.     var fileList = category.list(filter);
  269.     if (fileList.length != 0) {
  270.       fileInfo.ids = new Array();
  271.       fileInfo.names = new Array();
  272.       var fileObj = new File('temp');
  273.       for (var i=0; i < fileList.length; i++) {
  274.         fileObj.setPath(category.getAbsolutePath() + File.separator + fileList[i]);
  275.         if (fileList[i].search(/\d\d\d_/)==0 && fileObj.canWrite()) {
  276.           fileInfo.names.push(getSimpleName(fileList[i]));
  277.           idInfo = new Array();
  278.           idInfo.value = fileList[i];
  279.           idInfo.image = removeExtension(fileObj.getAbsolutePath()) + ".gif";
  280.           idInfo.template = fileObj.getAbsolutePath();
  281.           fileInfo.ids.push(idInfo);
  282.       } }
  283.       if (fileInfo.ids.length == 0) fileInfo.ids = null;
  284.     }
  285.   }
  286.   if (fileInfo.ids == null) {
  287.     fileInfo.ids = new Array('');
  288.     fileInfo.names = new Array(LABEL_noObjects);
  289.   }
  290.   OBJ_LIST.setInfo(fileInfo.names, fileInfo.ids);
  291.   if (OBJ_LIST.pickValue(OBJ_PICK) && OBJ_PICK)
  292.     OBJ_TYPE[0].checked = true;
  293.   else
  294.     OBJ_TYPE[1].checked = true;
  295. }
  296.  
  297.  
  298. function initializeUI() {
  299.   var theObj, theSel, templateFile, index, browserType;
  300.   var docDOM = dw.getDocumentDOM();
  301.   
  302.   if (!regCheck()) {
  303.     window.close();
  304.     return;
  305.   }
  306.  
  307.   setNewline(); //update NEWLINE global
  308.  
  309.   DEFAULT_IMG = FILE_authUrl + FILE_sep + "images" + FILE_sep + "custom_attain.gif";
  310.   
  311.   if (getEnclosingAttainObj() != null) {
  312.     theObj = getEnclosingAttainObj();
  313.     theSel = docDOM.nodeToOffsets(theObj);
  314.   } else {
  315.     theSel = docDOM.getSelection();
  316.   }
  317.   
  318.   FULL_SRC = getSelectedSrc(theSel);
  319.   
  320.   // get the template for the first selected object
  321.   KO_LIST = getSelectedAttainObjs(theSel);
  322.   templateFile = KO_LIST[0].getAttribute(TEMPLATE_ATTR);
  323.   if (templateFile) {
  324.     index = templateFile.indexOf('/');
  325.     if (index != -1) {
  326.       CAT_PICK = templateFile.substring(0, index);
  327.       OBJ_PICK = templateFile.substring(index+1);
  328.   } }
  329.   // get the highest browser type
  330.   browserType = 3;
  331.   for (var i=0; i < KO_LIST.length; i++) {
  332.     templateFile = KO_LIST[i].getAttribute(TEMPLATE_ATTR);
  333.     if (templateFile && templateFile.indexOf("_04.agt") != -1)
  334.       browserType = 4;
  335.   }
  336.   
  337.   CAT_TYPE = findObject('catType');
  338.   CAT_LIST = new PickList("catSel");
  339.   CAT_NAME = findObject('catName');
  340.   BROWSER_TYPE = findObject('browserType');
  341.   OBJ_TYPE = findObject('objType');
  342.   OBJ_LIST = new PickList("objSel");
  343.   OBJ_NAME = findObject('objName');
  344.   IMG_ICON = findObject('objIcon');
  345.   
  346.   // select the browser compatibility based on the selected object
  347.   if (OBJ_PICK) {
  348.     if (browserType == 4)
  349.       BROWSER_TYPE[0].checked = true;
  350.     else
  351.       BROWSER_TYPE[1].checked = true;
  352.   }
  353.   
  354.   setCategoryList();
  355.   setObjectList();
  356.    
  357.   if (OBJ_TYPE[0].checked)
  358.     IMG_ICON.src = OBJ_LIST.id.image;
  359.     
  360.   if (CAT_TYPE[1].checked) {
  361.     CAT_NAME.focus();
  362.     CAT_NAME.select();
  363.   } else if (OBJ_TYPE[1].checked) {
  364.     OBJ_NAME.focus();
  365.     OBJ_NAME.select();
  366.   }
  367. }
  368.  
  369.  
  370. function updateUI(itemName) {
  371.   if (itemName == 'catType') {
  372.     if (CAT_TYPE[0].checked) { // clear out the enter field
  373.       CAT_NAME.value = '';
  374.     }
  375.     setObjectList();
  376.     updateUI('objType');
  377.   } else if (itemName == 'catSel') {
  378.     CAT_LIST.change();
  379.     CAT_TYPE[0].checked = true;
  380.     updateUI('catType');
  381.   } else if (itemName == 'catName') {
  382.     if (isNonBlank(CAT_NAME.value)) {
  383.       var val1 = legalizeObjName(CAT_NAME.value);
  384.       CAT_NAME.value = val1
  385.         
  386.       CAT_TYPE[1].checked = true;
  387.       updateUI('catType');
  388.     }
  389.   } else if (itemName == 'browserType') {
  390.     setObjectList();
  391.     updateUI('objType');
  392.   } else if (itemName == 'objType') {
  393.     if (OBJ_TYPE[0].checked && !OBJ_LIST.id) {
  394.       OBJ_TYPE[1].checked = true;
  395.     } else if (OBJ_TYPE[0].checked) {  // clear out the enter fields
  396.       OBJ_NAME.value = '';
  397.       IMG_ICON.src = OBJ_LIST.id.image;
  398.       IMG_NAME = '';
  399.     } else {
  400.       if (IMG_NAME)
  401.         IMG_ICON.src = IMG_NAME;
  402.       else
  403.         IMG_ICON.src = DEFAULT_IMG;
  404.     }
  405.   } else if (itemName == 'objSel') {
  406.     OBJ_LIST.change();
  407.     OBJ_TYPE[0].checked = true;
  408.     OBJ_PICK = OBJ_LIST.id.value;
  409.     updateUI('objType');
  410.   } else if (itemName == 'objName') {
  411.     if (isNonBlank(OBJ_NAME.value)) {
  412.       val1 = legalizeObjName(OBJ_NAME.value);
  413.       OBJ_NAME.value = val1
  414.       
  415.       OBJ_TYPE[1].checked = true;
  416.     }
  417.   } else if (itemName == 'imgBrowse') {
  418.     var result = dreamweaver.browseForFileURL("select", "Select Gallery Icon", true);
  419.     if (result) {
  420.       var image = new File(result);
  421.       IMG_NAME = image.getAbsolutePath();
  422.       IMG_ICON.src = IMG_NAME;
  423.     }
  424.   }
  425. }
  426.  
  427. function legalizeObjName(theStr) {
  428.   var retVal="";
  429.   retVal = theStr.replace(/[\W]/g, "");      // remove non-word chars
  430.   retVal = retVal.replace(/G(\d\d)/g, "g$1"); // remove G## matches
  431.   if (retVal == 'length') retVal = 'lngth';   // reserved word
  432.   return retVal
  433. }
  434.